package com.xhtt.modules.app.controller.photo;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.msg.service.MsgUserService;
import com.xhtt.modules.photo.entity.PhotoProblem;
import com.xhtt.modules.photo.entity.PhotoRepairs;
import com.xhtt.modules.photo.entity.PhotoStagePhoto;
import com.xhtt.modules.photo.service.IPhotoProblemService;
import com.xhtt.modules.photo.service.IPhotoRepairsService;
import com.xhtt.modules.photo.service.IPhotoStagePhotoService;
import com.xhtt.modules.photo.vo.CountVO;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 拍照综合查询
 *
 * @Date 2019/2/28 17:53
 **/

@RestController
@RequestMapping("app/photo/composite")
@Api(value = "现场拍照-综合查询", tags = "现场拍照-综合查询")
public class PhotoCompositeController {

    @Autowired
    private IPhotoStagePhotoService photoStagePhotoService;

    @Autowired
    private IPhotoProblemService photoProblemService;

    @Autowired
    private IPhotoRepairsService photoRepairsService;

    @Autowired
    private MsgUserService msgUserService;

    @Autowired
    private SysUserService sysUserService;

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    @GetMapping(value = "/getCount")
    @ApiOperation("现场拍照-综合查询-获取装车台,问题记录,报修记录条数(app)")
    @Login
    public R getCount(@ApiIgnore @LoginUser SysUserEntity user) {
        LocalDateTime start = DateUtils.now();
        LocalDateTime end = start.plusDays(1);

        List<Integer> userIds = sysUserService.queryAuth(user);
        int stageCount = photoStagePhotoService.count(new LambdaQueryWrapper<PhotoStagePhoto>()
                .ge(PhotoStagePhoto::getCreateTime, start)
                .le(PhotoStagePhoto::getCreateTime, end)
                .in(PhotoStagePhoto::getCreateUserId, userIds));

        int problemCount = photoProblemService.count(new LambdaQueryWrapper<PhotoProblem>()
                .ge(PhotoProblem::getCreateTime, start)
                .le(PhotoProblem::getCreateTime, end)
                .in(PhotoProblem::getCreateUserId, userIds));

        int repairs = photoRepairsService.count(new LambdaQueryWrapper<PhotoRepairs>()
                .ge(PhotoRepairs::getCreateTime, start)
                .le(PhotoRepairs::getCreateTime, end)
                .in(PhotoRepairs::getCreateUserId, userIds));

        return R.ok().put(Constant.DATA, new CountVO(stageCount, problemCount, repairs));

    }

    @GetMapping(value = "/stageList")
    @ApiOperation("现场拍照-综合查询-获取装车台拍照列表(app)")
    @Login
    public R getStage(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        List<Integer> userIds = sysUserService.queryAuth(user);
        params.put("userIds", userIds);
        return R.ok().put(Constant.DATA, photoStagePhotoService.queryPage(params));
    }

    @GetMapping(value = "/problemList")
    @ApiOperation("现场拍照-综合查询-获取问题记录列表(app)")
    @Login
    public R getProblem(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        List<Integer> userIds = sysUserService.queryAuth(user);
        params.put("userIds", userIds);
        return R.ok().put(Constant.DATA, photoProblemService.queryPage(params));
    }

    @GetMapping(value = "/repairsList")
    @ApiOperation("现场拍照-综合查询-获取保修记录列表(app)")
    @Login
    public R getRepairs(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        List<Integer> userIds = sysUserService.queryAuth(user);
        params.put("userIds", userIds);
        return R.ok().put(Constant.DATA, photoRepairsService.queryPage(params));
    }

    @GetMapping(value = "/stageListForWeb")
    @ApiOperation("现场拍照-综合查询-获取装车台拍照列表(没有权限限制)(web)")
    @Login
    public R getStageForWeb(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, photoStagePhotoService.queryPage(params));
    }

    @GetMapping(value = "/problemListForWeb")
    @ApiOperation("现场拍照-综合查询-获取问题记录列表(没有权限限制)(web)")
    @Login
    public R getProblemForWeb(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, photoProblemService.queryPage(params));
    }

    @GetMapping(value = "/repairsListForWeb")
    @ApiOperation("现场拍照-综合查询-获取保修记录列表(没有权限限制)(web)")
    @Login
    public R getRepairsForWeb(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, photoRepairsService.queryPage(params));
    }


    @PostMapping(value = "/saveStage")
    @ApiOperation("现场拍照-综合查询-装车台拍照保存(app)")
    @Login
    public R addStage(@RequestBody PhotoStagePhoto photoStagePhoto, @ApiIgnore @LoginUser SysUserEntity user) {
        // 设置照片
        if (CollectionUtils.isNotEmpty(photoStagePhoto.getFileInfoModels())) {
            photoStagePhoto.setPhotoFile(JSON.toJSONString(photoStagePhoto.getFileInfoModels()));
        }
        photoStagePhotoService.save(photoStagePhoto);
        photoStagePhotoService.addMsg(photoStagePhoto, user);
        return R.ok();
    }

    @PostMapping(value = "/saveProblem")
    @ApiOperation("现场拍照-综合查询-问题记录保存(app)")
    @Login
    public R addProblem(@RequestBody PhotoProblem photoProblem, @ApiIgnore @LoginUser SysUserEntity user) {
        // 设置照片
        if (CollectionUtils.isNotEmpty(photoProblem.getFileInfoModels())) {
            photoProblem.setPhotoFile(JSON.toJSONString(photoProblem.getFileInfoModels()));
        }
        photoProblemService.save(photoProblem);
        photoStagePhotoService.addMsg(photoProblem, user);
        return R.ok();
    }

    @PostMapping(value = "/saveRepairs")
    @ApiOperation("现场拍照-综合查询-报修记录保存(app)")
    @Login
    public R addRepairs(@RequestBody PhotoRepairs photoRepairs, @ApiIgnore @LoginUser SysUserEntity user) {
        // 设置照片
        if (CollectionUtils.isNotEmpty(photoRepairs.getFileInfoModels())) {
            photoRepairs.setPhotoFile(JSON.toJSONString(photoRepairs.getFileInfoModels()));
        }
        photoRepairsService.save(photoRepairs);
        photoStagePhotoService.addMsg(photoRepairs, user);
        return R.ok();
    }

    @PostMapping(value = "/updateRepairs")
    @ApiOperation("现场拍照-综合查询-报修记录修改（目前只修改报修编号web）")
    public R updateRepairs(@RequestBody PhotoRepairs photoRepairs) {
        photoRepairsService.updateById(photoRepairs);
        return R.ok();
    }

    @GetMapping(value = "/exportStage")
    @ApiOperation("装车台导出")
    @Login
    public void exportStage(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        photoStagePhotoService.exportExcel(response, params);
    }

    @GetMapping(value = "/exportProblem")
    @ApiOperation("问题导出")
    @Login
    public void exportProblem(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        photoProblemService.exportExcel(response, params);
    }

    @GetMapping(value = "/exportRepairs")
    @ApiOperation("报修导出")
    @Login
    public void exportRepairs(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        photoRepairsService.exportExcel(response, params);
    }
}
